#!/bin/sh
#(c) Copyright 2006 Barry Kauler www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v3.01 BK 11oct07: bugfix, crash when choose usb button.
#BK v3.98: improve create mono font in status log.
#v425 rerwin: read device IDs from modules.alias and dialup-modem rules, instead of pci/usbmaps; choose last match, corresponding to backend_modprobe.
#v431 rerwin: Add --silent option to only generate files, without display.
#100108 BK 'scanpci' is not working properly xorg 7.5. actually it is no longer part of libpciaccess pkg.
#101221 yaf-splash fix.
#110814 01micko: convert script from gtkdialog2 to gtkdialog3+.
#110816 distinguish between inbuilt drivers and modules.
#110910 BK: gtkdialog3 (patriot-e version) syntax error, bump to gtkdialog4.
#120202 BK: internationalized.
#130326 3.5+ kernel no longer supports /proc/bus/usb, use script usb-devices.
#130331 change "Mono 12" to "Mono 10", for 96 dpi.
#140211 zigbert: gui (gtkdialog) improvements.

export TEXTDOMAIN=pupscan
export OUTPUT_CHARSET=UTF-8

[ "`echo $@ | grep ' *--silent *'`" != "" ] && SILENT=true || SILENT=false #v431
KERNVER="`uname -r`"
ZDRV='' #set in PUPSTATE.
#variables created at bootup by init script in initrd...
. /etc/rc.d/PUPSTATE
#v4.02 common functions...
. /etc/rc.d/functions4puppy4

if [ "$SILENT" != "true" ];then #100108
 /usr/lib/gtkdialog/box_splash -close never -text "$(gettext 'Please wait, probing hardware...')" &
 YSPID=$!
fi

ALL_MODULES="`cat /lib/modules/${KERNVER}/modules.dep | cut -f 1 -d ':' | rev | cut -f 1 -d '/' | cut -f 2 -d '.' | rev | tr '-' '_'`" #110816
echo "$ALL_MODULES" > /tmp/pupscan_all_modules #110816

MODITEMS=""
for ONEMOD in `lsmod | grep -v '^Module ' | cut -f 1 -d ' ' | tr '\n' ' '`
do
 MODITEMS="$MODITEMS<item>$ONEMOD</item>"
done

#v4.02 all mods now present...
PCIMODINFO="`cat /lib/modules/$KERNVER/modules.alias | tr -s " " | grep '^alias pci:' | sed -e 's/alias pci:v/0x/' \
-e 's/^0x\*/0xffffffff/' \
-e 's/d\(0000....\)sv[^ ]*/ 0x\1/' \
-e 's/d\*sv[^ ]*/ 0xffffffff/' \
 | tr [A-F] [a-f]`"
[ -s /etc/udev/rules.d/??-dialup-modem.rules ] && RULEMODINFO="`cat /etc/udev/rules.d/??-dialup-modem.rules | tr -s " " | grep '^ATTR{vendor}==' | sed -e 's/ATTR{vendor}==\"0x\(....\)\",/0x0000\1/' \
-e 's/ATTR{device}==\"0x\([^\"]*\)\",.*RUN[^ ]*/0x0000\1/' \
-e 's/0x0000[^ ]*\[.*\][^ ]*/0xffffffff/' \
-e 's/\"$//'`" || RULEMODINFO=""
[ "$RULEMODINFO" != "" ] && PCIMODINFO="$RULEMODINFO
$PCIMODINFO"

echo -n "" > /tmp/chipnummod.txt

#100108 puppy builds now have the full pci.ids file...
if [ ! -e /usr/share/pci.ids ];then
 #this is the old code...
 if [ "`which scanpci`" = "" ];then
  echo "$(gettext 'PCI scan utility scanpci not available. This must be a
cutdown Puppy (scanpci is part of the Xorg package).
Simplified information only is displayed here:')
"  >> /tmp/chipnummod.txt
  elspci -l  >> /tmp/chipnummod.txt
 else
  scanpci |
  while read ONELINE
  do
   VENDOR="`echo -n "$ONELINE" | grep '^pci' | grep -o 'vendor .*' | cut -f 2 -d ' ' | cut -f 2 -d 'x'`"
   CHIPNO="`echo -n "$ONELINE" | grep '^pci' | grep -o 'device .*' | cut -f 2 -d ' ' | cut -f 2 -d 'x'`"
   if [ "$VENDOR" ];then
    read NEXTLINE
    CHIPDESC="`echo -n "$NEXTLINE" | cut -f 2 -d '[' | cut -f 2 -d ']'`"
    LVENDOR="0x0000$VENDOR"
    LCHIPNO="0x0000$CHIPNO"
    APATTERN="$LVENDOR $LCHIPNO"
    FNDMOD="`echo "$PCIMODINFO" | grep "$APATTERN" | tail -n 1`" #v425
    MODULE="unknown"
    [ ! "$FNDMOD" = "" ] && MODULE="`echo -n "$FNDMOD" | cut -f 3 -d " "`" #v425
    if [ "$MODULE" = "unknown" ];then
     #find out if it is a usb module...
     UPATTERN=" ${VENDOR}:${CHIPNO} "
     [ ! "`elspci -l | grep -i "$UPATTERN" | grep '0C0300'`" = "" ] && MODULE="uhci-hcd"
     [ ! "`elspci -l | grep -i "$UPATTERN" | grep '0C0310'`" = "" ] && MODULE="ohci-hcd"
     [ ! "`elspci -l | grep -i "$UPATTERN" | grep '0C0320'`" = "" ] && MODULE="ehci-hcd"
    fi
    echo "DESCRIPTION: $CHIPDESC" >> /tmp/chipnummod.txt
    if [ "`echo "$MODULE" | tr '-' '_' | grep -x -f /tmp/pupscan_all_modules`" != "" ];then #110816
     echo "VENDOR: $VENDOR  DEVICE: $CHIPNO  KERNEL MODULE: $MODULE" >> /tmp/chipnummod.txt
    else
     echo "VENDOR: $VENDOR  DEVICE: $CHIPNO  KERNEL DRIVER (builtin): $MODULE" >> /tmp/chipnummod.txt
    fi
    echo  >> /tmp/chipnummod.txt
   fi
  done
 fi
else
 FIRSTSET=true #110816 rerwin: improve spacing.
 lspci -nn -k | tr '|' ' ' | tr -s ' ' | tr '\t' '|' | grep -v 'Subsystem: ' |
 while read ONELINE
 do
  if [ "`echo "$ONELINE" | cut -c 1`" != "|" ];then
   [ $FIRSTSET = true ] || echo >> /tmp/chipnummod.txt #110816 rerwin
   DESCRIPTION="DESCRIPTION: `echo -n "${ONELINE}" | cut -f 2-99 -d ' ' | rev | cut -f 2-9 -d '[' | rev | sed -e 's% \[.*\]:%:%'`"
   echo "$DESCRIPTION"  >> /tmp/chipnummod.txt
   CHIPNUMS="VENDOR: `echo -n "$ONELINE" | rev | cut -f 1 -d '[' | cut -f 2 -d ']' | rev | sed -e 's%:%  DEVICE: %'`"
   echo "$CHIPNUMS"  >> /tmp/chipnummod.txt
   FIRSTSET=false #110816 rerwin
  else
   MODULE="`echo "$ONELINE" | rev | cut -f 1 -d ':' | cut -f 1 -d ' ' | rev`"
   if [ "`echo "$MODULE" | tr '-' '_' | grep -x -f /tmp/pupscan_all_modules`" != "" ];then #110816
    KMODULE="KERNEL MODULE: ${MODULE}"
   else
    KMODULE="KERNEL DRIVER (builtin): ${MODULE}"
   fi
   echo "$KMODULE"  >> /tmp/chipnummod.txt
   #echo  >> /tmp/chipnummod.txt
  fi
 done
fi

[ ! -s /tmp/chipnummod.txt ] && echo "No PCI interfaces" > /tmp/chipnummod.txt #v3.01

#     <action>/tmp/formatfunc.sh 30</action>

#v2.17 usb devices...
USBSECTION=""
echo -n "" > /tmp/usbchipnummod.txt
#130326 3.5+ kernel no longer supports /proc/bus/usb, use script usb-devices...
USBDEVICES=""
[ -e /proc/bus/usb ] && USBDEVICES="`cat /proc/bus/usb/devices`"
[ ! "$USBDEVICES" ] && [ "`which usb-devices`" != "" ] && USBDEVICES="`usb-devices`" #usb-devices is script in usbutils pkg.

if [ "$USBDEVICES" ];then

 #v4.02...
 USBMODINFO="`cat /lib/modules/$KERNVER/modules.alias | tr -s " " | grep '^alias usb:' | sed -e 's/alias usb:v/0x/' -e 's/^0x\*/0xffff/' -e 's/p\(....\)d[^ ]*/ 0x\1/' -e 's/p\*d[^ ]*/ 0xffff/' | tr [A-F] [a-f]`"
 #...returns module-name vendor-id product-id

 USBNUMS="`echo "$USBDEVICES" | grep '^P: ' | tr -s ' ' | cut -f 2-3 -d ' ' | tr ' ' '|' | tr [A-Z] [a-z] | tr '\n' ' '`"
 for ONENUM in $USBNUMS
 do
  VENDOR="`echo -n "$ONENUM" | cut -f 1 -d '|' | cut -f 2 -d '='`"
  PRODUCT="`echo -n "$ONENUM" | cut -f 2 -d '|' | cut -f 2 -d '='`"
  ONEUSB="0x${VENDOR} 0x${PRODUCT}"
   
  [ "$ONEUSB" = "0x0000 0x0000" ] && continue
   
  YESPARA=no
  USBMODULE=""
  rm -f /tmp/usbmodulexxx
  echo -n "" > /tmp/usbdevdescr
  echo "$USBDEVICES" | tr -s ' ' |
  while read ONELINE
  do
   zPATTERN="Vendor=${VENDOR} ProdID=${PRODUCT}"
   [ "`echo -n "$ONELINE" | grep "$zPATTERN"`" != "" ] && YESPARA=yes
   if [ "$YESPARA" = "yes" ];then
    xDRIVER="`echo -n "$ONELINE" | grep '^I:' | grep -o ' Driver=.*' | cut -f 2 -d '='`"
    [ "$xDRIVER" != "" ] && echo -n "$xDRIVER" > /tmp/usbmodulexxx
    xDESCR="`echo -n "$ONELINE" | grep '^S:' | cut -f 2-20 -d ' '`"
    [ "$xDESCR" != "" ] && echo "$xDESCR" >> /tmp/usbdevdescr
    [ "$ONELINE" = "" ] && break
   fi
  done
  [ ! -s /tmp/usbdevdescr ] && [ "`grep ' ' /tmp/usbmodulexxx`" != "" ] && echo "Product=`cat /tmp/usbmodulexxx`" > /tmp/usbdevdescr && rm /tmp/usbmodulexxx #v425
  [ -f /tmp/usbmodulexxx ] && USBMODULE="`cat /tmp/usbmodulexxx`"
   
  [ "$USBMODULE" = "" ] && USBMODULE="`echo "$USBMODINFO" | grep "$ONEUSB" | cut -f 3 -d ' ' | tail -n 1`" #v425

  cat /tmp/usbdevdescr >> /tmp/usbchipnummod.txt
  if [ "`echo "$USBMODULE" | tr '-' '_' | grep -x -f /tmp/pupscan_all_modules`" != "" ];then #110816
   echo "VendorID=$VENDOR  ProductID=$PRODUCT  KERNEL-MODULE=$USBMODULE" >> /tmp/usbchipnummod.txt
  else
   echo "VendorID=$VENDOR  ProductID=$PRODUCT  KERNEL-DRIVER(builtin)=$USBMODULE" >> /tmp/usbchipnummod.txt
  fi
  echo  >> /tmp/usbchipnummod.txt
   
 done

 [ ! -s /tmp/usbchipnummod.txt ] && echo "No plugged-in USB devices" > /tmp/usbchipnummod.txt #v3.01

 USBSECTION='
 <hseparator></hseparator>
 <hbox border-width="7" space-expand="true" space-fill="true">
   <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext '<b>USB devices</b>
USB interfaces are part of the PCI interfaces, but to find information about any plugged-in USB devices, click this button.')'"</label></text>
   <text space-expand="false" space-fill="false"><label>""</label></text>
   <vbox space-expand="false" space-fill="false">
     <button>
       '"`/usr/lib/gtkdialog/xml_button-icon plug_usb.svg big`"'
       <action>cp -f /tmp/usbchipnummod.txt /tmp/hwproberesult.txt</action>
       <action>refresh:INFO</action>
     </button>
   </vbox>
 </hbox>'
fi

PupScan='
<window title="'$(gettext 'Puppy hardware-interfaces information')'" icon-name="gtk-preferences">
<vbox>
  <vbox space-expand="false" space-fill="false">
    <frame>
      '"`/usr/lib/gtkdialog/xml_info fixed hardware_detect.svg 60 " " "<b>$(gettext "Find information about your hardware")</b>"`"'
      <vbox border-width="7" space-expand="false" space-fill="false">
        <hbox space-expand="true" space-fill="true">
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext '<b>Kernel modules</b>
At bootup, Puppy examines the hardware interfaces to determine what kernel drivers (modules) to load, and does so. Click this button for information on loaded modules.')'"</label></text>
          <text space-expand="false" space-fill="false"><label>""</label></text>
          <vbox space-expand="false" space-fill="false">
            <button>
              '"`/usr/lib/gtkdialog/xml_button-icon module.svg big`"'
              <action>eval /tmp/hwprobelsmod</action>
              <action>refresh:INFO</action>
            </button>
          </vbox>
        </hbox>
        <hbox space-expand="true" space-fill="true">
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext 'More <b>details</b> on each loaded module.')'"</label></text>
          <combobox>
            <variable>MODCOMBO</variable>
            '$MODITEMS'
          </combobox>
          <text space-expand="false" space-fill="false"><label>""</label></text>
          <vbox space-expand="false" space-fill="false">
            <button>
              '"`/usr/lib/gtkdialog/xml_button-icon module_yes.svg big`"'
              <action>eval /tmp/hwprobemodinfo \$MODCOMBO</action>
              <action>refresh:INFO</action>
            </button>
          </vbox>
        </hbox>
      </vbox>
      <hseparator></hseparator>
      <hbox border-width="7" space-expand="true" space-fill="true">
        <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext '<b>PCI interfaces</b>
Most of the hardware interfaces inside a PC are 'PCI' devices. Click this button for a scan of all the PCI interfaces.')'"</label></text>
        <text space-expand="false" space-fill="false"><label>""</label></text>
        <vbox space-expand="false" space-fill="false">
          <button>
            '"`/usr/lib/gtkdialog/xml_button-icon card_pci.svg big`"'
            <action>cp -f /tmp/chipnummod.txt /tmp/hwproberesult.txt</action>
            <action>refresh:INFO</action>
          </button>
        </vbox>
      </hbox>
     '${USBSECTION}'
    </frame>
  </vbox>
  <vbox space-expand="true" space-fill="true">
    <edit space-expand="true" space-fill="true">
      <variable>INFO</variable>
      <input file>/tmp/hwproberesult.txt</input>
    </edit>
  </vbox>
  <hbox space-expand="false" space-fill="false">
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <label>'$(gettext 'Quit')'</label>
      <action type="exit">EXIT_NOW</action>
    </button>
  </hbox>
</vbox>
</window>'

rm -f /tmp/hwproberesult.txt #v425 erase stale info 

if [ "`which scanpci`" = "" ];then
 echo 'echo "This must be a cutdown Puppy that does not have the scanpci utility
(which is part of the Xorg package). So, cannot give a detailed
report on the PCI bus. Instead can display this summary only. The
most useful information here is the vendor:chip numbers (column-3):
" >/tmp/hwproberesult.txt
lspci -n >>/tmp/hwproberesult.txt'  >/tmp/hwprobescanpci
else
 echo 'scanpci >/tmp/hwproberesult.txt' >/tmp/hwprobescanpci
fi
chmod 755 /tmp/hwprobescanpci

echo 'lsmod >/tmp/hwproberesult.txt' >/tmp/hwprobelsmod
chmod 755 /tmp/hwprobelsmod
echo 'modinfo $1 > /tmp/hwproberesult.txt 2>&1' >/tmp/hwprobemodinfo
chmod 755 /tmp/hwprobemodinfo

#v3.98 improved...
#130331 change Mono 12 to Mono 10...
echo 'style "specialmono"
{
  font_name="Mono 10"
}

class "GtkText*" style "specialmono"' > /tmp/gtkrc_mono
export GTK2_RC_FILES=/tmp/gtkrc_mono:/root/.gtkrc-2.0

[ "$SILENT" != "true" ] && kill $YSPID #100108

if [ $SILENT != true ]; then
 . /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
 RETSTRING="`echo "$PupScan" | gtkdialog --stdin`"
fi

###END###
